import { Component } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { _HttpClient } from '@delon/theme';
import { NzMessageService } from 'ng-zorro-antd/message';
import { NzModalRef } from 'ng-zorro-antd/modal';

@Component({
  selector: 'app-sys-user-ip-limit',
  templateUrl: './ip-limit.component.html'
})
export class SysUserIpLimitComponent {
  record: any = {};
  i: any;
  allowIp?: string = '';
  banIp?: string = '';
  validateForm: FormGroup;
  controlsConfig = {
    allowIp: ['', []],
    banIp: ['', []]
  };
  companies = [];

  constructor(private modal: NzModalRef, private msgSrv: NzMessageService, public http: _HttpClient, private fb: FormBuilder) {
    this.validateForm = this.fb.group(this.controlsConfig);
  }

  ngOnInit(): void {
    console.log(this.record);
    //console.log(this.i);
    this.allowIp = this.record.allow_ip;
    this.banIp = this.record.ban_ip;
  }

  save(value: any): void {
    if (this.record.ID) {
      value.ID = this.record.ID;
      this.http.put(`api/user/ip_limit`, value).subscribe(res => {
        if (res.err_code != 0) {
          this.msgSrv.error(res.msg);
          return;
        }
        this.msgSrv.success(res.msg);
        this.modal.close(true);
      });
    }
  }

  close(): void {
    this.modal.destroy();
  }
}
